package com.aidex.generator.service.impl;

import com.aidex.common.core.service.impl.BasicServiceImpl;
import com.aidex.common.exception.BizException;
import com.aidex.common.utils.NumberUtils;
import com.aidex.generator.domain.GenConfigTemplate;
import com.aidex.generator.mapper.GenConfigTemplateMapper;
import com.aidex.generator.service.GenConfigTemplateService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional(readOnly = true)
@Service
public class GenConfigTemplateServiceImpl extends BasicServiceImpl<GenConfigTemplateMapper, GenConfigTemplate> implements GenConfigTemplateService {

    @Override
    public int insert(GenConfigTemplate genConfigTemplate) {
        checkTemplateNameUnique(genConfigTemplate);
        return super.insert(genConfigTemplate);
    }

    @Override
    public void checkTemplateNameUnique(GenConfigTemplate template) {
        QueryWrapper<GenConfigTemplate> wrapper = new QueryWrapper<>();
        wrapper.eq("template_name", template.getTemplateName());
        wrapper.eq("id", template.getId());
        if (mapper.selectCount(wrapper) > 0) {
            throw new BizException("模板名称已存在，请重新输入！");
        }
    }

    @Override
    public int findMaxSort() {
        return NumberUtils.nextOrder(mapper.findMaxSort());
    }
}